<template>
  <div class="goods-container">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索和添加商品区域 -->
      <el-row :gutter="20">
        <!-- 搜索列 -->
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="clearInput">
            <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮列表 -->
        <el-col :span="3">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表表格区域 -->
      <el-table :data="goodslist" border stripe style="width: 100%">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100"> </el-table-column>
        <el-table-column label="创建时间" width="180">
          <template #default="scope">
            <span>{{ scope.row.add_time | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="20">
          <template #default="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="gotoEdit(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页栏 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="queryInfo.pagenum" :page-sizes="[10, 20, 40, 100]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Goods',
  data() {
    return {
      // 存储商品列表数据
      goodslist: [],
      // 商品的总数量
      total: 0,
      // 请求商品列表数据参数对象
      queryInfo: {
        query: '', // 查询参数。与搜索框进行双向数据绑定
        pagenum: 1, // 当前页码
        pagesize: 10 // 每页显示条数
      }
    }
  },
  methods: {
    // 初始化商品列表数据
    async initGoodsList() {
      const { data: res } = await this.$http({
        method: 'GET',
        url: '/goods',
        params: this.queryInfo
      })
      if (res.meta.status !== 200) this.$message.error('获取商品列表数据失败: ' + res.meta.msg)
      // 保存商品列表数据
      this.goodslist = res.data.goods
      // 保存商品总数
      this.total = res.data.total
      // console.log(res.data)
    },
    // 点击了搜索按钮
    searchGoods() {
      if (this.queryInfo.query === '') return
      this.initGoodsList()
    },
    // 清空了搜索框中的内容
    clearInput() {
      this.initGoodsList()
    },
    // 每页显示条数 发生改变时触发
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.initGoodsList()
    },
    // 当前 页码值 发生改变时触发
    handleCurrentChange(pageNum) {
      this.queryInfo.pagenum = pageNum
      this.initGoodsList()
    },
    // 根据ID删除指定的商品
    async delGoods(row) {
      // 弹框询问用户是否进行删除
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      // 取消删除
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      // 发起请求根据指定ID删除用户
      const { data: res } = await this.$http({
        method: 'DELETE',
        url: '/goods/' + row.goods_id
      })
      if (res.meta.status !== 200) return this.$message.error('删除商品失败!请稍后再试~')
      this.$message.success('删除商品成功!')
      this.initGoodsList()
    },
    // 点击了添加商品按钮
    goAddPage() {
      this.$router.push('/goods/add')
    },
    // 点击了修改商品按钮
    gotoEdit(row) {
      this.$router.push('/goods/' + row.goods_id)
    }
  },
  created() {
    this.initGoodsList()
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 15px 0;
  font-size: 12px;
}
</style>
